package com.tanhua.dubbo.server.controller;

import com.tanhua.dubbo.server.enums.CommentPublishIdType;
import com.tanhua.dubbo.server.service.QuanZiService;
import com.tanhua.dubbo.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: QuanZiCommentController
 * @Author:王晨晗
 * @Description:
 * @Date: 2022/01/02/22:08
 */
@RestController
@RequestMapping("comments")
public class QuanZiCommentController {
    @Autowired
    private QuanZiService quanZiService;

    /**
     * @description 获取评论列表
     * @param publishId     动态id
     * @param page      当前页
     * @param pageSize      每页显示条数
     * @return
     */
    @GetMapping
    public ResponseEntity queryCommentsList(@RequestParam("movementId") String publishId,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = quanZiService.queryCommentList(publishId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * @description 保存发布评论
     * @param param 参数列表
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param) {
        String publishId = param.get("movementId");     //动态id
        String content = param.get("comment");      //文本内容
        quanZiService.saveComments(publishId, content , CommentPublishIdType.PUBLISH);      //保存评论,指定是动态的评论
        //响应结果
        return ResponseEntity.ok(null);
    }

    /**
     * @description 动态的评论点赞
     * @param publishId 评论id
     * @return
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        //调用点赞方法,指明是评论的点赞
        Long likeCount = quanZiService.likeComment(publishId , CommentPublishIdType.COMMENT);
        //返回点赞数
        return ResponseEntity.ok(likeCount);
    }

    /**
     * @description 动态评论的取消点赞
     * @param publishId 评论的id
     * @return
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        //调用取消点赞方法;
        Long likeCount = quanZiService.disLikeComment(publishId);
        //返回点赞数
        return ResponseEntity.ok(likeCount);
    }

}
